package cn.globalph.housekeeper.widgets.loadingview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import cn.globalph.housekeeper.R;


/**
 * Created by yuan on 2017/10/27 0027.
 */

public class LoadingFooterView extends FrameLayout {
    private final View mLoadingView;
    private final View mEndView;
    private Status mStatus;

    public LoadingFooterView(@NonNull Context context) {
        this(context, null);
    }

    public LoadingFooterView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingFooterView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context)
                .inflate(R.layout.loading_footer, this, true);
        mLoadingView = findViewById(R.id.loading_view);
        mEndView = findViewById(R.id.end_view);
        setStatus(Status.GONE);
    }

    public boolean canLoadMore() {
        return mStatus == Status.GONE;
    }

    public Status getStatus() {
        return mStatus;
    }

    public void setStatus(Status status) {
        this.mStatus = status;
        change();
    }

    private void change() {
        switch (mStatus) {
            case EMPTY:
                this.setVisibility(GONE);
                mLoadingView.setVisibility(GONE);
                mEndView.setVisibility(GONE);
                break;
            case GONE:
                this.setVisibility(VISIBLE);
                mLoadingView.setVisibility(GONE);
                mEndView.setVisibility(GONE);
                break;

            case LOADING:
                this.setVisibility(VISIBLE);
                mLoadingView.setVisibility(VISIBLE);
                mEndView.setVisibility(GONE);
                break;

            case THE_END:
                this.setVisibility(VISIBLE);
                mLoadingView.setVisibility(GONE);
                mEndView.setVisibility(VISIBLE);
                break;
            case THE_END_NO_DES:
                // 不显示，但占据位置，防止最后一行紧贴底边
                this.setVisibility(VISIBLE);
                mLoadingView.setVisibility(GONE);
                mEndView.setVisibility(GONE);
        }
    }

    public enum Status {
        EMPTY,GONE, LOADING, THE_END, THE_END_NO_DES
    }
}
